﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Students.Bases;

namespace Students
{
    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loginAlert.Visible = false;
        }

        protected void AlertError(string msg)
        {
            loginAlert.Attributes["class"] = "alert alert-danger";
            loginAlert.InnerHtml = msg;
            loginAlert.Visible = true;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            string userName = tbUsername.Text;
            string passWord = tbPassword.Text;
            string confirmPassword = tbCofirmPassword.Text;
            string firstName = tbFirstName.Text;
            string lastName = tbLastName.Text;
            string age = tbAge.Text;
            bool isFullTime = cbIsFullTime.Checked;
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord) || string.IsNullOrEmpty(confirmPassword) ||
                string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(age))
            {
                AlertError("Please input all fields");
                return;
            }
            if (passWord != confirmPassword)
            {
                AlertError("Confirm password incorrect");
                return;
            }

            int aGe = int.MinValue;
            if (!int.TryParse(age, out aGe))
            {
                AlertError("Age is integer");
                return;
            }

            BOStudents std = new BOStudents
            {
                Age = aGe,
                Firstname = firstName,
                IsFullTime = isFullTime,
                Lastname = lastName,
                Password =passWord,
                Username = userName
            };
            BLStudents bStd = new BLStudents();

            if (bStd.Insert(ref std))
            {
                Session["OAuth"] = std;
                Response.Redirect(ResolveUrl("~/Courses.aspx"));
            }
            else
            {
                AlertError("Register fail");
                return;
            }
        }
    }
}